/**
 * 工具： 获取当前时间 年/月/日/时/分
 */

export function currentTime (type) { //type 默认返回完整地址 2.年月日
  var date = new Date(); // 获取当前时间
  var year = date.getFullYear(); // 年
  var month = date.getMonth() + 1; // 月
  var day = date.getUTCDate(); // 日
  var hours = date.getHours(); // 时
  var min = date.getMinutes(); // 分
  var sec = date.getSeconds(); // 秒
  if (month < 10) month = '0' + month
  if (day < 10) day = '0' + day
  if (hours < 10) hours = '0' + hours
  if (min < 10) min = '0' + min
  if (sec < 10) sec = '0' + sec
  var datepage = year + '-' + month + '-' + day + ' ' + hours + ':' + min + ':' + sec
  if (type == 2) datepage = year + '-' + month + '-' + day
  return datepage
}


/**
 * 工具： 获取七天后的时间 年/月/日/时/分
 */
// 获取当前时间，day为number，getDay(-1):昨天的日期;getDay(0):今天的日期;getDay(1):明天的日期;【以此类推】
export function sevenDaysLater (day,type) { // type 等于0返回完整的当前日期 1返回月日时分 9返回对象 2 返回年月日
  let type_ = type || 0
  var today = new Date();
  var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
  today.setTime(targetday_milliseconds); //注意，这行是关键代码
  var tYear = today.getFullYear();
  var tMonth = today.getMonth() + 1;
  var tDate = today.getDate();
  var tHours = today.getHours(); // 时
  var tMin = today.getMinutes(); // 分
  var tSec = today.getSeconds(); // 秒
  if (tMonth < 10) tMonth = '0' + tMonth
  if (tDate < 10) tDate = '0' + tDate
  if (tHours < 10) tHours = '0' + tHours
  if (tMin < 10) tMin = '0' + tMin
  if (tSec < 10) tSec = '0' + tSec
  if (type_ == 0) return tYear + "-" + tMonth + "-" + tDate + ' ' + tHours + ':' + tMin + ':' + tSec
  if (type_ == 2) return tYear + "-" + tMonth + "-" + tDate
  if (type_ == 1) return tMonth + "/" + tDate + ' ' + tHours + ':' + tMin
  if (type_ == 9) return {tYear: tYear,tMonth:tMonth,tDate:tDate,tHours:tHours,tMin:tMin,tSec:tSec}
}

// 获取2个时间戳时间差
export function getRemainderTime (s1,s2) {
  let timestamp1 = new Date(s1.replace(/-/g, "/"))
  let timestamp2 = new Date(s2.replace(/-/g, "/"))
  let runTime = parseInt((timestamp2.getTime() - timestamp1.getTime()) / 1000)
  let year = Math.floor(runTime / 86400 / 365);
  runTime = runTime % (86400 * 365);
  let month = Math.floor(runTime / 86400 / 30);
  runTime = runTime % (86400 * 30);
  let day = Math.floor(runTime / 86400);
  runTime = runTime % 86400;
  let hour = Math.floor(runTime / 3600);
  runTime = runTime % 3600;
  let minute = Math.floor(runTime / 60);
  runTime = runTime % 60;
  let second = runTime;
  if(year<0 || month<0 || day<0 && hour<0 || minute<0 || second<0){
    return {
      code: 201,
      year: '00',
      month: '00',
      day: '00',
      hour: '00',
      minute: '00',
      second: '00'
    }
  }
  let data = {
    code: 200,
    year: year,
    month: month,
    day: day,
    hour:  (hour < 10)? '0' + hour : hour,
    minute: (minute < 10)? '0' + minute : minute,
    second: (second < 10)? '0' + second : second
  }
  return data
}

// 转换时间为，上午，下午，昨天，和日期，类似微信聊天时间
export function msgToggleTime(s1){
  let timestamp1 = new Date(s1)
  let timestamp2 = new Date()
  const oldtYear = timestamp1.getFullYear(); // 年
  const oldtMonth = timestamp1.getMonth() + 1; // 月
  const oldtDate = timestamp1.getDate(); // 日
  const oldHours = timestamp1.getHours(); // 时
  const oldtMin = timestamp1.getMinutes(); // 分
  const newtYear2 = timestamp2.getFullYear(); // 年
  const newtMonth2 = timestamp2.getMonth() + 1; // 月
  const newtDate2 = timestamp2.getDate(); // 日
  const difference = (oldtYear+String(oldtMonth)+oldtDate) - (newtYear2+String(newtMonth2)+newtDate2)
  if (difference === 0 && oldHours < 12) { //今天上午
    return `上午${oldHours}:${oldtMin < 10 ? '0'+oldtMin : oldtMin}`
  }else if(difference === 0 && oldHours === 12){
    return `中午${oldHours}:${oldtMin < 10 ? '0'+oldtMin : oldtMin}`
  }else if(difference === 0 && oldHours > 18){
    return `晚上${oldHours}:${oldtMin < 10 ? '0'+oldtMin : oldtMin}`
  }else if(difference === 0 && oldHours > 12){
    return `下午${oldHours}:${oldtMin < 10 ? '0'+oldtMin : oldtMin}`
  }else if (difference === 1){
      return '昨天'
  }else{
    return `${oldtMonth}月${oldtDate}日`
  }
}
